<script>
  import {col, row} from '@/components/Flex';
  export default {
    computed: {
      showdate: function () {
        let dateobj = {};
        dateobj.startTime = this.getStartTime(this.date);
        dateobj.endTime = this.getEndTime(this.date);
        dateobj.type = this.datatype;
        /*var callNow = !this.timeout;
        if (this.timeout) clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          this.timeout = null;
          this.$emit('getTabBarDatep', dateobj);
        }, 3000);
        if (callNow) { console.log(1); this.$emit('getTabBarDatep', dateobj); }*/
        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          this.$emit('getTabBarDatep', dateobj);
        }, 400);
        //(debounce(this.$emit, 2000, true))('getTabBarDatep', dateobj);
        if (+this.datatype === 2) {
          return this.formatDate(this.getFirstDayOfWeek(this.date)) + '--' + this.formatDate(this.getFirstDayOfWeek(this.date) + 518400000);
        } else {
          return this.formatDate(this.date);
        }
      }
    },
    props: {
      startTime: {
        type: Number,
        default () {
          let num = Date.parse(new Date());
          return num;
        }
      },
      startDateType: {
      }
    },
    created () {
      this.date = this.startTime;
      this.datatype = +this.startDateType || this.datatype;
    },
    methods: {
      //获取年月日方法
      getY_M_D: function (date = new Date()) {
        if (typeof date === 'number') {
          date = new Date(date);
        }
        var obj = {
          year: date.getFullYear(),
          month: date.getMonth(),
          date: date.getDate()
        };
        return obj;
      },
      //判断是否是将来的时间
      isFuture: function (time) {
        let nowDate = Date.parse(new Date());
        return this.getStartTime(time) > nowDate;
      },
      //获取endTime的方法
      getEndTime: function (e) {
        var timeObj = this.getY_M_D(e);
        switch (+this.datatype) {
          case 1:
            return this.getDateNum(timeObj.year, timeObj.month, timeObj.date + 1);
          case 2:
            var day = new Date(e).getDay() || 7;
            return this.getDateNum(timeObj.year, timeObj.month, timeObj.date + 7 - day + 1);
          case 3:
            return this.getDateNum(timeObj.year, timeObj.month + 1, 1);
          case 4:
            return this.getDateNum(timeObj.year + 1, 0, 1);
        }
      },
      getStartTime: function (e) {
        var timeObj = this.getY_M_D(e);
        switch (+this.datatype) {
          case 1:
            return this.getDateNum(timeObj.year, timeObj.month, timeObj.date);
          case 2:
            var day = new Date(e).getDay() || 7;
            return this.getDateNum(timeObj.year, timeObj.month, timeObj.date - day + 1);
          case 3:
            //.log(this.getDateNum(timeObj.year, timeObj.month, 1));
            return this.getDateNum(timeObj.year, timeObj.month, 1);
          case 4:
            return this.getDateNum(timeObj.year, 0, 1);
        }
      },
      //选取type，初始化时间  更改type
      _pick: function (e) {
        let pickdate = this.getY_M_D();
        switch (+e.target.dataset.type) {
          case 2:
            pickdate = this.getDateNum(pickdate.year, pickdate.month, pickdate.date);
            this.date = this.getFirstDayOfWeek(pickdate);
            break;
          case 3:
            pickdate = this.getDateNum(pickdate.year, pickdate.month);
            this.date = pickdate;
            break;
          case 4:
            pickdate = this.getDateNum(pickdate.year);
            this.date = pickdate;
            break;
          default:
            pickdate = this.getDateNum(pickdate.year, pickdate.month, pickdate.date);
            this.date = pickdate;
        }
        this.datatype = e.target.dataset.type;
      },
      //更改时间格式方法
      formatDate: function (now) {
        let formatdate = this.getY_M_D(now);
        formatdate.month = formatdate.month + 1;
        let returnDate = '';
        switch (+this.datatype || 1) {
          case 1:
            returnDate = formatdate.year + '-' + formatdate.month + '-' + formatdate.date;
            break;
          case 2:
            returnDate = formatdate.year + '-' + formatdate.month + '-' + formatdate.date;
            break;
          case 3:
            returnDate = formatdate.year + '-' + formatdate.month;
            break;
          case 4:
            returnDate = formatdate.year;
            break;
          default:
//            alert('???');
        }
        return returnDate;
      },
      //获取周区间的方法
      getFirstDayOfWeek: function (date) {
        let newdate = new Date(date);
        var day = newdate.getDay() || 7;
        return +new Date(newdate.getFullYear(), newdate.getMonth(), newdate.getDate() + 1 - day);
      },
      //加减时间计算方法
      getDateNum: function (year, month = 0, date = 1) {
        var d = Date.parse(new Date(year, month, date));
        return d;
      },
      //左翻方法
      leftArrow: function () {
        switch (+this.datatype || 1) {
          case 1:
            this.date = this.date - 60 * 60 * 24 * 1000;
            break;
          case 2:
            this.date = this.date - 60 * 60 * 24 * 1000 * 7;
            break;
          case 3:
            let leftDate = this.getY_M_D(this.date);
            leftDate.month = leftDate.month - 1;
            if (leftDate.month < 0) {
              leftDate.year = leftDate.year - 1;
              leftDate.month = 11;
            }
            this.date = this.getDateNum(leftDate.year, leftDate.month);
            break;
          case 4:
            let leftDate2 = this.getY_M_D(this.date);
            leftDate2.year = leftDate2.year - 1;
            this.date = this.getDateNum(leftDate2.year);
            break;
          default:
            alert('???');
        }
      },
      //右翻方法
      rightArrow: function () {
        //console.log(this.getEndTime(this.date) > Date.parse(new Date()), this.getEndTime(this.date), Date.parse(new Date()));
        if (this.getEndTime(this.date) > new Date()) return;
        switch (+this.datatype || 1) {
          case 1:
            this.date = this.date + 60 * 60 * 24 * 1000;
            break;
          case 2:
            this.date = this.date + 60 * 60 * 24 * 1000 * 7;
            break;
          case 3:
            let rightDate = this.getY_M_D(this.date);
            rightDate.month = rightDate.month + 1;
            if (rightDate.month > 11) {
              rightDate.year = rightDate.year + 1;
              rightDate.month = 0;
            }
            this.date = this.getDateNum(rightDate.year, rightDate.month);
            break;
          case 4:
            let rightDate2 = this.getY_M_D(this.date);
            rightDate2.year = rightDate2.year + 1;
            this.date = this.getDateNum(rightDate2.year);
            break;
          default:
            alert('???');
        }
      }
    },
    activated () {
      this.date = this.startTime;
      this.datatype = +this.startDateType || this.datatype;
      let dateobj = {};
      dateobj.startTime = this.getStartTime(this.date);
      dateobj.endTime = this.getEndTime(this.date);
      dateobj.type = this.datatype;
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        this.$emit('getTabBarDatep', dateobj);
      }, 0);
    },
    components: {
      'dc-col': col,
      'dc-row': row
    }
  };
</script>
